import Common from '../common'
var Js = Common('torder')
export default {
	name: 'torder',
	mixins: [Js],
	components: {},
	data() {
		return {
			fields: [{
					key: 'id',
					label: 'ID',
					application: [],
					componentName: 'sls-input',
					listType: '',
					width: 50
				},
				{
					key: 'order_no',
					label: '订单NO',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width: 150,
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入订单NO',
						clearable: true,
						defaultValue: '',
						optionsName: '',
						changeFunc: function(val, self) {
							if (val) {
								self.searchItem.order_no = val;
							} else {
								delete self.searchItem.order_no;
							};

						},
					},
				},
				{
					key: 'group_no',
					label: '团购NO',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width: 150,
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入团购NO',
						clearable: true,
						defaultValue: '',
						optionsName: '',
						changeFunc: function(val, self) {
							if (val) {
								self.searchItem.group_no = val;
							} else {
								delete self.searchItem.group_no;
							};

						},
					},
				},
				{
					key: 'price',
					label: '订单金额',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width:120
				},
				{
					key: 'f_price',
					label: '运费',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width:120
				},
				{
					key: 'standard',
					label: '成团人数',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width:100
				},
				{
					key: "pay_status",
					label: '支付状态',
					application: ['编辑'],
					type: 'select',
					listType: 'normal',
					formatter: function(val, tests) {
						if (val.pay_status == -1) {
							return '退款'
						} else if (val.pay_status === 0) {
							return '否'
						} else if (val.pay_status === 1) {
							return '是'
						}
					},
					componentName: 'sls-select',
					optionsName: 'payOptions',
					filter_multiple: false,
					listType: 'normal',
					defaultProps: {
						label: 'text',
						value: 'value',
					},
					placeholder: '请选择是否支付',
					width: 80,
					header_search: {
						componentName: 'sls-select',
						defaultValue: '',
						optionsName: 'payOptions',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请选择是否支付',
						changeFunc: function(val, self) {
							if (val || val === 0) {
								self.searchItem.pay_status = val;
							} else {
								delete self.searchItem.pay_status;
							};
						},
					}
				},
				{
					key: "transport_status",
					label: '配送/核销状态',
					application: ['编辑'],
					type: 'select',
					listType: 'normal',
					formatter: function(val, tests) {
						return ['未发货', '配送中', '已收货'][val.transport_status];
					},
					componentName: 'sls-select',
					optionsName: 'transportOptions',
					filter_multiple: false,
					defaultProps: {
						label: 'text',
						value: 'value',
					},
				},
				{
					key: "order_step",
					label: '订单状态',
					application: [],
					type: 'select',
					listType: 'normal',
					formatter: function(val, tests) {
						if (val.order_step == -1) {
							return '拒绝撤单'
						} else {
							return ['正常下单', '申请撤单', '已撤单','完结'][val.order_step];
						}
					},
					componentName: 'sls-select',
					optionsName: 'stepOptions',
					filter_multiple: false,
					defaultProps: {
						label: 'text',
						value: 'value',
					},
				},
				{
					key: 'shop_no',
					label: '下单商家',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width: 150,
					formatter:function(val){
						return val.shop.name
					},
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入商家名称',
						clearable: true,
						changeFunc: function(val, self) {
							if (val) {
								self.UserInfo.searchItem.shop_no = ['LIKE',['%'+val+'%']];
							} else {
								delete self.UserInfo.searchItem.shop_no;
							};
							self.beforeSearch('UserInfo');
						},
					},
				},
				{
					key: "transport_type",
					label: '取货方式',
					application: [],
					type: 'select',
					listType: 'normal',
					formatter: function(val, tests) {
						if(val.transport_type==1){
							return '快递'
						}else {
							return '自提'
						}
					},
					defaultProps: {
						label: 'text',
						value: 'value',
					},
					width:100,
					header_search: {
						componentName: 'sls-select',
						optionsName: 'typeOptions',
						placeholder: '请选择取货方式',
						changeFunc: function(val, self) {
							if (val || val === 0) {
								self.searchItem.transport_type = val;
							} else {
								delete self.searchItem.transport_type;
							};
						},
					}
				},
				{
					key: "group_status",
					label: '参团状态',
					application: [],
					type: 'select',
					listType: 'normal',
					formatter: function(val, tests) {
						return ['未开团', '已开团'][val.group_status-1];
					},
					componentName: 'sls-select',
					optionsName: 'stepOptions',
					filter_multiple: false,
					defaultProps: {
						label: 'text',
						value: 'value',
					},
				},
				{
					key: "express_company",
					label: '快递公司',
					application: ['编辑'],
					componentName: 'sls-select',
					optionsName: 'expressOptions',
					defaultProps: {
						label: 'title',
						value: 'title',
					}
				},
				{
					key: 'express_no',
					label: '快递单号',
					application: ['编辑'],
					componentName: 'sls-input',
				},
				{
					key: 'user_no',
					label: '用户NO',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
				},
				{
					key: 'name',
					label: '用户昵称',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					formatter: function(val, tests) {
						return val.User.nickname ? val.User.nickname : '';
					},
				},
				{
					key: 'name',
					label: '姓名',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入姓名',
						clearable: true,
						changeFunc: function(val, self) {
							if (val) {
								self.searchItem.name = ['LIKE',['%'+val+'%']];
							} else {
								delete self.searchItem.name;
							};
						},
					},
				},
				{
					key: 'phone',
					label: '电话',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入电话',
						clearable: true,
						changeFunc: function(val, self) {
							if (val) {
								self.searchItem.phone = ['LIKE',['%'+val+'%']];
							} else {
								delete self.searchItem.phone;
							};
						},
					},
				},
				// {
				// 	key: 'label_str',
				// 	label: '商品分类',
				// 	componentName: 'sls-cascader',
				// 	optionsName: 'labelOptions',
				// 	limit: 10,
				// 	defaultProps: {
				// 		children: 'children',
				// 		label: 'title',
				// 		value: 'id',
				// 	},
				// 	placeholder: '请选择分类',
				// 	header_search: {
				// 		componentName: 'sls-cascader',
				// 		optionsName: 'labelOptions',
				// 		style: 'width:160px;margin-right:2px;',
				// 		placeholder: '请选择分类',
				// 		defaultValue: [],
				// 		changeFunc: function(val, self) {
				// 			var arr = self.$$getChildNames(self.optionData.labelOptions, 'id', 'children', val,
				// 				[]);
				// 			if (val) {
				// 				if (arr.length > 0) {
				// 					var sres = ['like'];
				// 					for (var i = 0; i < arr.length; i++) {
				// 						sres.push("%" + arr[i] + "%")
				// 					};
				// 					sres.push('or');
				// 					self.searchItem.label_str = sres;
				// 				} else {
				// 					self.searchItem.label_str = ['like', '%' + val + '%'];
				// 				}
				// 			} else {
				// 				delete self.searchItem.label_str;
				// 			};

				// 		},
				// 	}
				// },
				{
					key: 'product_str',
					label: 'product_str',
					componentName: 'sls-input',
					listType: '',
					header_search: {
						componentName: 'sls-input',
						placeholder: '请输入商品名称',
						suggest: true,
						querySearch: function(val, self) {
							console.log('querySearch')
							if (val) {
								var postData = {
									searchItem: {
										title: ['like', '%' + val + '%'],
										status: 1
									}
								};
								var res = self.initProductData(postData);
								return res;
							} else {
								delete self.searchItem.product_str;
							}


						},
						changeFunc: function(val, self) {
							if (val) {
								if (val.indexOf(",") > -1) {
									var arr = val.split(',');
									var sres = [];
									for (var i = 0; i < arr.length; i++) {
										sres.push("%" + arr[i] + "%")
									};
									self.searchItem.product_str = ['like', sres, 'OR'];
								} else {
									self.searchItem.product_str = ['like', ['%' + val + '%'], 'OR'];
								};
							} else {
								delete self.searchItem.product_str;
							};

						},
					}
				},
				{
					key: 'create_time',
					label: '创建时间',
					listType: 'normal',
					placeholder: '请选择创建时间',
					header_search: {
						componentName: 'sls-date-time-range',
						style: 'width:160px;margin-right:2px;',
						start_placeholder: '创建开始时间',
						end_placeholder: '创建结束时间',
						defaultValue: '',
						changeFunc: function(value, self) {
							if (!value) {
								delete self.searchItem.create_time;
							} else {
								self.searchItem.create_time = ['between', value = value.map(function(e) {
									return e / 1000;
								})]
							};

						},
					},
					width: 200,
				},
				{
					label: '操作',
					listType: 'deal',
					width: 200
				},
			],

			// 按钮配置
			btn_info: [
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'list',
					text: function(data) {
						return '编辑'
					},
					func: {
						apiName: function(self) {
							return "api_orderUpdate"
						},
						formData: function(self) {
							return self.formData;
						},
						postData: function(self) {
							var postData = {
								searchItem: {
									id: self.formData.id,
									user_type: 0,
								},
								data: self.submitData
							};
							return postData;
						}
					},
				},
				{
					type: 'danger',
					icon: 'delete',
					size: 'mini',
					funcType: 'submit',
					position: 'header',
					text: function(data) {
						return '删除选中'
					},
					func: {
						apiName: function(self) {
							return "api_orderUpdate"
						},
						postData: function(self) {
							var deleteArray = [];
							for (var i = 0; i < self.selectionArray.length; i++) {
								deleteArray.push(self.selectionArray[i].id);
							};
							var postData = {
								searchItem: {
									id: ['in', deleteArray],
									user_type: 0,
								},
								data: {
									status: -1
								}
							};
							return postData;
						}
					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'header',
					func: {
						func: function(self) {
							self.excelOutput(self);
						},
					},
					text: function(data) {
						return '导出'
					},
					funcType: 'func',
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					funcType: 'func',
					position: 'list',
					text: function(data) {
						return '退款'
					},
					styleFunc: function(data) {
						if (data.order_step == 1) {
							return 'background-color: #2c9a7f;border-color:#2c9a7f'
						} else {
							return 'display: none'
						}
					},
					func: {
						formData: function(self) {
							return self.formData;
						},
						func: function(self) {
							self.refundOrder(self.formData);
						},
					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'list',
					funcType: 'submit',
					text: function(data) {
						return '拒绝'
					},
					styleFunc: function(data) {
						if (data.order_step == 1) {
							return 'background-color: #F56C6C;border-color:#F56C6C'
						} else {
							return 'display: none'
						}
					},
					func: {
						apiName: function(self) {
							return "api_orderUpdate"
						},
						formData: function(self) {
							return self.formData;
						},
						postData: function(self) {
							var postData = {
								searchItem: {
									order_no: self.formData.order_no
								},
								data: {
									order_step: -1
								}
							};
							postData.saveAfter = [{
								tableName: 'Order',
								FuncName: 'update',
								data: {
									order_step: -1
								},
								searchItem: {
									parent_no: self.formData.order_no
								}
							}]
							return postData;
						}
					},
				},
			],

			searchItem: {
				level:2,
				order_type:3
			},

			startSearchItem: {
				level:2,
				order_type:3
			},
			optionData: {
				typeOptions:[
					{text:'快递',value:1},
					{text:'自提',value:2},
				],
				payOptions: [{
					text: '未支付',
					value: 0
				}, {
					text: '已支付',
					value: 1
				}, {
					text: '退款',
					value: -1
				}],
				transportOptions: [{
					text: '未发货/待自提',
					value: 0
				}, {
					text: '配送中',
					value: 1
				}, {
					text: '已收货',
					value: 2
				}],
				stepOptions: [{
					text: '正常下单',
					value: 0
				}, {
					text: '申请撤单',
					value: 1
				}, {
					text: '已撤单',
					value: 2
				}, {
					text: '完结',
					value: 3
				}],
				behaviorOptions: [{
					text: '采购',
					value: 1
				}, {
					text: '仓储',
					value: 2
				}],
				labelOptions: [],
				expressOptions:[]
			},
			otherData: {},
			UserInfo: {
				tableName: 'UserInfo',
				searchItem: {},
				key: 'user_no',
				middleKey: 'user_no',
				condition: 'in',
			}

		}

	},

	mounted() {
		const self = this;
		self.table_arguments.tree_props = {
			children: 'child',
			child: 'child',
			hasChildren: 'child'
		}
		self.table_arguments.expand = true;
		if(this.$store.state.user.userinfo.role!=3){
			self.searchItem.shop_no = this.$store.state.user.userinfo.user_no;
			self.startSearchItem.shop_no = this.$store.state.user.userinfo.user_no;
		}
		this.init()
	},
	computed: {
		token: function() {
			return this.$store.getters.getToken
		}
	},
	methods: {

		/**
		 * 初始化
		 */
		init() {
			this.initMainData();
			this.initLabel();
			this.initExpress();
		},


		async excelOutput(self) {

			if (self.btnName == '导出') {
				const postData = {};
				postData.token = self.$store.getters.getToken;
				if (self.searchItem) {
					postData.searchItem = self.$$cloneForm(self.searchItem)
					postData.searchItem.level = 2
				};
				postData.excelOutput = {
					expTitle: 'test',
					expCellName: [{
							title: '订单编号',
							key: ['order_no'],
							type: 'string',
						},
						{
							title: '订单金额',
							key: ['price'],
							type: 'string',
						},
						{
							title: '支付状态(1：已支付 0：未支付)',
							key: ['pay_status'],
							type: 'string',
						},
						{
							title: '购买商品',
							key: ['child', 0, 'title'],
							type: 'string',
						},
						{
							title: '地区',
							key: ['snap_address', 'city'],
							type: 'string',
						},
						{
							title: '详细地址',
							key: ['snap_address', 'detail'],
							type: 'string',
						},
						{
							title: '收货人',
							key: ['snap_address', 'name'],
							type: 'string',
						},
						{
							title: '联系电话',
							key: ['snap_address', 'phone'],
							type: 'string',
						},
					],
					fileName: 'test'
				};

				window.open('https://test.solelyplus.com/275/public/index.php/api/v1/Common/Order/get?token=' +
					postData.token +
					'&searchItem=' +
					JSON.stringify(postData.searchItem) +
					'&excelOutput=' +
					JSON.stringify(postData.excelOutput))
			}

		},

		/**
		 * 列表主函数
		 */
		async initMainData(isNew) {

			const self = this;
			self.table_arguments.loading = true;
			const postData = {};
			if (isNew) {
				self.paginate.currentPage = 1;
			};
			postData.paginate = self.$$cloneForm(self.paginate);
			postData.token = self.$store.getters.getToken;
			if (self.searchItem) {
				postData.searchItem = self.$$cloneForm(self.searchItem)
			};
			if (JSON.stringify(self.getBefore) != "{}") {
				postData.getBefore = self.$$cloneForm(self.getBefore);
			};
			postData.getAfter = {
				User: {
					tableName: 'User',
					middleKey: 'user_no',
					key: 'user_no',
					condition: '=',
					searchItem: {
						status: 1
					},
					info: ['nickname'],
				},
				shop: {
					tableName: 'UserInfo',
					middleKey: 'shop_no',
					key: 'user_no',
					condition: '=',
					searchItem: {status: ['in',[-1,1]]},
					info: ['name'],
				},
			};

			var res = await self.$$api_orderGet({
				data: postData
			});
			self.mainData = res.info.data;
			self.paginate.count = res.info.total;
			self.table_arguments.loading = false;

		},

		/**
		 * 列表主函数
		 */
		async initLabel() {
			const self = this;
			const postData = {};
			postData.token = self.$store.getters.getToken;
			postData.searchItem = {
				type: 2,
				parentid:5
			};
			var res = await self.$$api_labelGet({
				data: postData
			});
			self.optionData.labelOptions = res.info.data;
		},
		
		async initExpress() {
			const self = this;
			const postData = {};
			postData.token = self.$store.getters.getToken;
			postData.searchItem = {
				menu_id:32
			};
			var res = await self.$$api_articleGet({
				data: postData
			});
			self.optionData.expressOptions = res.info.data;
		},

		async initProductData(postData) {

			const self = this;
			postData.token = self.$store.getters.getToken;
			var res = await self.$$api_productGet({
				data: postData
			});

			if (res.info.data.length > 0) {
				for (var i = 0; i < res.info.data.length; i++) {
					res.info.data[i]['value'] = res.info.data[i]['title'];
					res.info.data[i]['product_str'] = res.info.data[i]['id'];
				};
				return res.info.data;
			} else {
				return [{
					"value": "无",
					"product_str": ""
				}];
			};

		},


	},


}
